package com.best.bestadmin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.best.bestadmin.mapper.AdminUserMapper;
import com.best.bestadmin.mapper.AppUserMapper;
import com.best.bestadmin.mapper.CommentMapper;
import com.best.bestadmin.mapper.FoodMapper;
import com.best.bestadmin.pojo.AdminUser;
import com.best.pojo.AppUser;
import com.best.pojo.Food;
import com.best.pojo.FoodComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yangruyi
 * @date 2021/3/14
 */
@Service
public class AdminService {
    @Autowired
    private AppUserMapper userMapper;

    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private CommentMapper commentMapper;
    public List<AppUser> checkUsers(String name, Integer state) {
        QueryWrapper<AppUser> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<AppUser> la = wrapper.lambda().eq(AppUser::getUserState, state);
        if (!"".equals(name) && name != null) {
            la.like(AppUser::getUsername, name);
        }
        return userMapper.selectList(wrapper);
    }

    public boolean hateUser(String userId, Integer state) {
        QueryWrapper<AppUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AppUser::getId, userId);
        AppUser appUser = userMapper.selectById(userId);
        appUser.setUserState(state);
        return userMapper.updateById(appUser) >= 1;
    }

    public boolean delUser(String userId) {
        return userMapper.deleteById(userId) >= 1;
    }

    public List<Food> checkFoods(Integer state) {
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Food::getState, state);
        return foodMapper.selectList(wrapper);
    }

    public boolean auditFood(String foodId,Integer state){
        Food food = foodMapper.selectById(foodId);
        food.setState(state);
        return foodMapper.updateById(food) >= 1;
    }

    public boolean delFood(String foodId){
        return foodMapper.deleteById(foodId) >=1;
    }
    public List<FoodComment> checkComments(String words){
        QueryWrapper<FoodComment> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<FoodComment> la = wrapper.lambda().orderByDesc(FoodComment::getCreateTime);
        if(words != null){
            la.like(FoodComment::getContent,words);
        }
        return commentMapper.selectList(wrapper);
    }
    public boolean delComment(String id){
        return commentMapper.deleteById(id) >= 1;
    }
    public boolean editAdmin(AdminUser adminUser){
        return adminUserMapper.updateById(adminUser) >= 1;
    }
    public AdminUser authentication(String username){
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AdminUser::getUsername,username);
        return adminUserMapper.selectOne(wrapper);
    }
}
